package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Slf4j
@RequestMapping("/checkgroup")
public class CheckGroupController {
     @Autowired
     private CheckGroupService checkGroupService;

    /**
     * 分页查询
     * @return
     */
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
       log.info("检查组分页查询:{}",queryPageBean);
        PageResult pageResult = checkGroupService.findPage(queryPageBean);
        //封装返回结果
       return new Result(true, MessageConst.ACTION_SUCCESS, pageResult);
    }

    /**
     * 新增检查组
     */
    @PostMapping("add")
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds){
        log.info("新增检查组:{},检查项:{}",checkGroup,checkitemIds);
        checkGroupService.add(checkGroup,checkitemIds);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }

    /**
     * 根据id查询检查组的数据
     */
    @GetMapping("/findById")
    public Result getById(Integer id){
        log.info("根据id查询检查组的数据：{}", id);
        CheckGroup checkGroup = checkGroupService.getById(id);
        return new Result(true, MessageConst.ACTION_SUCCESS, checkGroup);
    }

    /**
     * 回显当前检查组包含的检查项
     * @param id
     * @return
     */
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        log.info("根据检查组id查询检查项id：{}", id);
       List<Integer> checkItemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        return new Result(true, MessageConst.ACTION_SUCCESS, checkItemIds);
    }

    /**
     * 更新检查组信息
     */
    @RequestMapping("/edit")
    public Result updateCheckGroup(@RequestBody CheckGroup checkGroup ,Integer[] checkitemIds){
        log.info("更新检查组信息：{},检查项：{}",checkGroup,checkitemIds);
        checkGroupService.updateCheckGroup(checkGroup,checkitemIds);
        return new Result(true, MessageConst.EDIT_CHECKITEM_SUCCESS);
    }

    /**
     * 删除检查组信息
     */
    @PostMapping("/deleteCheckGroupitemById")
    public Result deleteCheckGroupitemById(Integer id){
        log.info("删除检查组信息：{}", id);
        checkGroupService.deleteCheckGroupitemById(id);
        return new Result(true, MessageConst.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 检查组数据查询
     */
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("查询所有检查组数据");
        List<CheckGroup> checkGroups = checkGroupService.findAll();
        return new Result(true, MessageConst.ACTION_SUCCESS, checkGroups);
    }


}
